package com.share.cloudStorage.aspect;


import com.share.cloudStorage.filter.SecurityFilter;
import com.share.cloudStorage.logs.model.Logs;
import com.share.cloudStorage.logs.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;



@Component
@Aspect
public class LogAspect {
    // 创建 Map 集合
    static Map<String, String> methodDescriptionMap = new HashMap<>();

    // 添加方法名称及其中文描述
    static {
        methodDescriptionMap.put("login", "手机号和密码登录");
        methodDescriptionMap.put("logon", "手机号验证码登录");
        methodDescriptionMap.put("register", "用户注册");
        methodDescriptionMap.put("create", "创建分享链接");
        methodDescriptionMap.put("cancelShare", "取消分享");
        methodDescriptionMap.put("searchFilesById", "查询文件");
        methodDescriptionMap.put("deleteFiles", "删除文件");
        methodDescriptionMap.put("downLoadFile", "下载文件");
        methodDescriptionMap.put("fileRecycle", "文件放入回收站");
        methodDescriptionMap.put("uploadFile", "上传文件");
        methodDescriptionMap.put("reName", "重命名文件");
        methodDescriptionMap.put("createFolder", "创建文件夹");
    }

    @Autowired
    private LogService logService;
    @Pointcut("execution(* com.share.cloudStorage.*.controller.*Controller.*(..))")
    public void findMethods() {}

    @After("findMethods()")
    public void AfterReturning(JoinPoint jp) {
        String methodName = jp.getSignature().getName();
        System.out.println("After method: " + methodName);
        String msg = methodDescriptionMap.get(methodName);
        if (msg !=null){
            Integer integer = SecurityFilter.userid.get();
            if (integer!=null){
                logService.save(new Logs(integer,msg));
            }
        }
    }
}